package il.ac.tau.cg.ex3.parser.parametershander;

import il.ac.tau.cg.ex3.entities.scene.Scene;
import il.ac.tau.cg.ex3.entities.styles.Color;
import il.ac.tau.cg.ex3.math.Point;
import il.ac.tau.cg.ex3.math.Vector;
import il.ac.tau.cg.ex3.parser.Parser;
import il.ac.tau.cg.ex3.parser.Parser.ParseException;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

public abstract class EntityParametersHandler
{	
	protected List<String> paramsCache; 
	
	public abstract boolean addParam(String key, String[] value) throws ParseException;
	protected abstract boolean validateEntity() throws ParseException;
	protected abstract void createEntity(Scene scene) throws ParseException;
	
	public void commit(Scene scene) throws ParseException
	{
		if (validateEntity())
		{
			createEntity(scene);
		}
	}
	
	public EntityParametersHandler()
	{
		paramsCache = new ArrayList<String>();
	}
	
	protected static Color getColor(String[] param)
	{
		double r = Double.parseDouble(param[0]);
		double g = Double.parseDouble(param[1]);
		double b = Double.parseDouble(param[2]);
		
		return new Color(r, g, b);
	}
	
	protected static Vector getVector(String[] param)
	{
		double x = Double.parseDouble(param[0]);
		double y = Double.parseDouble(param[1]);
		double z = Double.parseDouble(param[2]);
		
		return new Vector(x, y, z);
	}
	
	protected static Point getPoint(String[] param)
	{
		double x = Double.parseDouble(param[0]);
		double y = Double.parseDouble(param[1]);
		double z = Double.parseDouble(param[2]);
		
		return new Point(x, y, z);
	}
	
	protected static BufferedImage getTexture(String[] param) throws ParseException
	{
		File srcFile = new File(param[0]);
		
		BufferedImage srcImgBuf = null; 
		try 
		{
			srcImgBuf = ImageIO.read(srcFile);
		}
		catch(IOException e)
		{
			Parser.pAssert(false, "IO EXCEPTION");
		}
		
		return srcImgBuf;
	}
}
